document.write("<script type='text/javascript' src='core/config.js'></script>");
document.write("<script type='text/javascript' src='core/common.js'></script>");

function gcd(x, y) {
    while (y != 0) {
        var z = x % y;
        x = y;
        y = z;
    }
    return x;
}

function gcdV(vArray) {
    var L = vArray.length;
    if (L == 1) {
        return vArray[0];
    }
    var g = vArray[1];
    for (var i = 0; i < L; i++) {
        g = gcd(g, vArray[i]);
    }
    return Math.abs(g);
}

// Working with matrices
function iRandomMatrix(m, n, r) {
    mA = Matrix.Random(m, n).x(r);
    mB = Matrix.Random(m, n).x(2*r);
    return mA.subtract(mB).round();
}

function MatrixOperations(matrix) {
    this.m = matrix;

    // Elementary matrix operations
    this.swapRows = function swapRows(nr1, nr2) {
        var n1 = nr1 - 1;
        var n2 = nr2 - 1;
        for (var i = 0; i < this.m.cols(); i++) {
            var temp = this.m.elements[n1][i];
            this.m.elements[n1][i] = this.m.elements[n2][i];
            this.m.elements[n2][i] = temp;
        }
    }

    this.divideRowBy = function divideRowBy(n) {
        for (var i = 0; i < this.m.cols(); i++) {
            this.m.elements[n][i] /= n;
        }
    }

    this.addRows = function addRows(nrSrc, nrDest, mul) {
        var n1 = nrSrc - 1;
        var n2 = nrDest - 1;
        for (var i = 0; i < this.m.cols(); i++) {
            this.m.elements[n2][i] = this.m.elements[n2][i] + this.m.elements[n1][i] * mul;
        }
    }

    this.makeZeroColumn = function (col) {
        var m = this.m;
        var n = this.m.cols();
        
        // if column 'col' has elements equal to zero below row = 'col'
        var nonZero = false;
        for (var i = col; i < n; i++) {
            if (m.e(col, i) != 0) {
                nonZero = true;
                break;
            }
        }
        if (!nonZero) {
            return this.m;
        }
    }
}

MatrixOperations.SWAP = "swap";
MatrixOperations.ADD = "add";
MatrixOperations.ADDMUL = "addmul";
MatrixOperations.NONE = "";

function LaTeXMatrix(slmatrix){
    this.m = slmatrix;
    this.toString = function toString() {
        var s = "\\begin{vmatrix}\n";
        for (var i = 1; i <= this.m.rows(); i++) {
            for (var j = 1; j <= this.m.cols(); j++) {
                if (j > 1) {
                    s += "&";
                }
               s += this.m.e(i,j);
            }
            s += "\\\\\n";
        }
        s += "\\end{vmatrix}";
        return s;
    };
};



